﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL.Interface;
using DAL.Models;

namespace DAL.Dao
{
    public class ManagerDaoimpl : BaseDao
    {
        public int ExistNum { get { using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions)) return context.Manager.Count(); } private set { } }

        public ManagerDaoimpl(bool use_clock = true) : base(use_clock)
        {
              
        }
        public IList<Manager> GetList()
        {
            using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions))
                return (from i in context.Manager select i).ToList();
        }

        public (int,IList<Manager>) GetList(int rows, int page)
        {
            using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions))
            {
                var rs = context.Manager.OrderBy(o => o.Id).ToList();
                return (GetTotalPages(rs.Count, rows), rs.Skip((page - 1) * rows).Take(rows).ToList());
            }
            
        }



        public Manager? GetItemByID(int id)
        {
            using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions))
                return (from i in context.Manager
                    where i.Id == id
                    select i).ToList().FirstOrDefault();
        }

        public bool Update(int id, Manager newitem)
        {
            using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions))
            {
                lock (clock_obj_update)
                {

                    Manager? item = (from i in context.Manager
                                     where i.Id == id
                                     select i).ToList().FirstOrDefault();
                    if (item == null)
                    {
                        return false;
                    }
                    //DbEntityEntry<Bag> entry = DbEntity.expressDBEntities.Entry<Bag>(newitem);
                    //3.设置该对象为被修改过
                    //entry.State = EntityState.Unchanged;
                    //4.设置该对象的 uName属性为修改状态，同时 entry.State 被修改为 Modified 状态
                    item.UserName = newitem.UserName;
                    item.UserPass = newitem.UserPass;
                    item.TrueName = newitem.TrueName;
                    item.Phone = newitem.Phone;
                    item.Idcard = newitem.Idcard;
                    item.UpdateTime = newitem.UpdateTime;
                    item.AddTime = newitem.AddTime;


                    context.SaveChanges();

                    return true;
                }

            }

        }

        public Manager? Find(int id)
        {
            using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions))
                return context.Manager.Find(id);
        }

        public bool Delete(int id)
        {
            using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions))
            {
                lock (clock_obj_delete)
                {

                    Manager manager = context.Manager.Find(id);
                    if (manager == null) return false;
                    context.Manager.Remove(manager);
                    context.SaveChanges();
                    return true;
                }
            }
                

        }
   

        public Manager? Login(string username, string userpass)
        {
            using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions))
                return (from i in context.Manager
                     where i.UserName.Trim().Equals(username) && i.UserPass.Trim().Equals(userpass)
                     select i).ToList().FirstOrDefault();
            
        }


        public bool Add(Manager manager)
        {
            using (ExpressDBContext context = new ExpressDBContext(DbEntity.DBContextOptions))
            {
                lock (clock_obj_add)
                {


                    var res = (from i in context.Manager where i.UserName.Equals(manager.UserName) select i).FirstOrDefault();
                    if (res != null) return false;
                    //manager.Id = GetList().Last().Id + 1;
                    context.Manager.Add(manager);
                    context.SaveChanges();
                    return true;
                }
            }
               

        }

    }
}
